$('.select2').select2();
// $.get("header_menu.html",function(data){ $("#header").html(data); });
$.get("footer.html",function(data){ $("#footer").html(data); });

function pic_replace(dom1,dom2){
    var file = document.getElementById(dom1).files;
    var reader = new FileReader();
    reader.readAsDataURL(file[0]);
    reader.onload = function(e){
        var Img = '<img class="act_img_sty" src="'+this.result+'"/>';
        $('.'+dom2).html(Img)
    }
}

$(document).ready(function(){
    // $("#active").addClass("active");
    // $("#active2").addClass("active");

    $('#add-guest').click(function(){

        var count = $('#more-guset').data('num');
        if(count>=6){
            layer.msg('最多添加五位嘉宾！');
          return;
        }
      console.log(count);
      var pic_id = 'guest_pic' + count;
        var guestName = 'guestName' + count;
        var guestIntroduce = 'guestIntroduce' + count;
        var guestPic = 'guestPic' + count;

        var guest = `<div class="col-lg-12">
                        <div class="col-lg-6 col-sm-12 itemList">
                           <div class="col-lg-12">
                              <span>嘉宾姓名</span>
                              <input type="text" name="${guestName}"/>
                            </div>
                           <div class="col-lg-12">
                                <span>嘉宾介绍</span>
                                <textarea maxlength="130" class="form-control guestExplain" rows="3" type="text" name="${guestIntroduce}" style="width: 80%;resize:none;vertical-align: top;"></textarea>
                            </div>
                          </div>
                          <div class="col-lg-6 col-sm-12 addImage">
                             <div class="col-lg-2 col-md-4">嘉宾照片</div>
                              <input id="${pic_id}" class="guest_pic_input" onchange="pic_replace(\'${pic_id}\',\'${pic_id}\')" type="file" name="${guestPic}"/>
                              <label class="${pic_id} guest_pic col-lg-5 col-md-6" for="${pic_id}">+</label>
                          </div>
                        </div>
                        `;
        $('#more-guset').append(guest);
      ++count;
      $('#more-guset').data('num',count);

    });

    $('#confirm').click(function(){
        var textOnoff = true;

        //判断必填信息
        if($('#activityName').val()==''&&textOnoff==true){
          textOnoff = false;
            layer.msg('请填写活动名称');

        }else if($('#activityExplain').val()==''&&textOnoff==true){
            textOnoff = false;
            layer.msg('请填写活动简介');

        }else if($('#selectCity').val()==''&&textOnoff==true){
            textOnoff = false;
            layer.msg('请填写活动所在城市');

        }else if($('#activityDate').val()==''&&textOnoff==true){
            layer.msg('选择活动时间');
            textOnoff =false;
        }else if($('.activity_pic').text()=='+'&&textOnoff==true){
            layer.msg('请添加活动详情介绍图');
            textOnoff =false;
        }else if($('#compereName').val()==''&&textOnoff){
            layer.msg('请填写主持人姓名');
            textOnoff =false;
        }else if($('#compereExplain').val()==''&&textOnoff){
            layer.msg('请填写主持人介绍');
            textOnoff =false;
        }else if($('.compere_pic').text()=='+'&&textOnoff){
            layer.msg('请选择主持人照片');
            textOnoff =false;
        }else if($('#guestName').val()==''&&textOnoff){
            layer.msg('请填写嘉宾姓名');
            textOnoff =false;
        }else if($('#guestExplain').val()==''&&textOnoff){
          layer.msg('请填写嘉宾介绍');
          textOnoff =false;
        }else if($('.guest_pic0').text()=='+'&&textOnoff){
          layer.msg('请选择嘉宾照片');
          textOnoff =false;
        }

        if (textOnoff) {
            var formData = new FormData(document.getElementById('activityInfo'));
            $.ajax({
                type:'POST',
                url:'http://114.116.72.220:9999/NewActivity/',
                async:true,
                data:formData,
                processData: false,
                contentType: false,
                success:data=>{
                    if(data == 'success'){
                        layer.msg('新建活动成功！', {time: 500}, function () {
                            location.reload();
                        });
                    }else{
                        layer.msg('新建活动失败！')
                    }
                }
            });
        }

    });
});
